package com.dongdongshop.service.impl;

import com.dongdongshop.domain.PageResult;
import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TypeTemplateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@DubboService
public class TypeTemplateServiceImpl implements TypeTemplateService {

    @Resource
    private TbTypeTemplateMapper typeTemplateMapper;

    @Override
    public PageResult<TbTypeTemplate> typetemplateList(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbTypeTemplate> list = typeTemplateMapper.selectByName(name);
        PageInfo<TbTypeTemplate> page = new PageInfo<>(list);
        PageResult pr = new PageResult();
        pr.setPageNum(pageNum);
        pr.setPageSize(pageSize);
        pr.setPages(page.getPages());
        pr.setTotal(page.getTotal());
        pr.setRows(page.getList());
        return pr;
    }

    @Override
    public void toAdd(TbTypeTemplate typeTemplate, String brandListJson, String specListJson, String kuozhanListJson) {
        TbTypeTemplate tt = new TbTypeTemplate();
        tt.setCustomAttributeItems(kuozhanListJson);
        tt.setBrandIds(brandListJson);
        tt.setSpecIds(specListJson);
        tt.setName(typeTemplate.getName());
        typeTemplateMapper.insertSelective(tt);
    }

    @Override
    public TbTypeTemplate selectById(Long id) {
        TbTypeTemplate tbTypeTemplate = typeTemplateMapper.selectByPrimaryKey(id);
        return tbTypeTemplate;
    }

    @Override
    public void updateInfo(TbTypeTemplate typeTemplate, String brandListJson, String specListJson, String kuozhanListJson) {
        TbTypeTemplate tt = new TbTypeTemplate();
        tt.setId(typeTemplate.getId());
        tt.setCustomAttributeItems(kuozhanListJson);
        tt.setBrandIds(brandListJson);
        tt.setSpecIds(specListJson);
        tt.setName(typeTemplate.getName());
        typeTemplateMapper.updateByPrimaryKeySelective(tt);
    }

    @Override
    public void deleteInfo(Long[] ids) {
        typeTemplateMapper.deleteById(ids);
    }
}
